package com.gzr.collection.stream;

import com.gzr.collection.entity.Teacher;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * stream流的统计，收集操作（终结方法）
 */
public class StreamDemo04 {
    public static void main(String[] args) {
        List<Teacher> teachers = new ArrayList<>();
        teachers.add(new Teacher("白眉鹰王",66,58888));
        teachers.add(new Teacher("金翼蝠王",56,48888));
        teachers.add(new Teacher("张三",26,8888));
        teachers.add(new Teacher("李四",23,9888));
        teachers.add(new Teacher("张飞",23,5888));

        //forEach
        teachers.stream().filter(v -> v.getSalary() > 10000).forEach(System.out::println);

        // count
        long count = teachers.stream().filter(v -> v.getSalary() > 10000).count();
        System.out.println(count);

        //获取薪水最高的老师对象
        Optional<Teacher> max = teachers.stream().max((t1, t2) -> Double.compare(t1.getSalary(), t2.getSalary()));
        Teacher maxTecher = max.get(); //获取Optional对象的元素
        System.out.println(maxTecher);

        //获取薪水最低的老师对象
        Optional<Teacher> min = teachers.stream().min((t1, t2) -> Double.compare(t1.getSalary(), t2.getSalary()));
        Teacher minTecher = min.get(); //获取Optional对象的元素
        System.out.println(minTecher);


        System.out.println("--------------");

        ArrayList<String> list = new ArrayList<>();
        list.add("张无忌");
        list.add("张三丰");
        list.add("张三");
        list.add("赵敏");

        //收集到List集合
        List<String> collect = list.stream().filter(v -> v.startsWith("张")).collect(Collectors.toList());
        System.out.println(collect);
        
        //收集到Set集合
        Set<String> collect1 = list.stream().filter(v -> v.startsWith("张")).collect(Collectors.toSet());
        System.out.println(collect1);

        //收集到数组中去
        Object[] array = list.stream().filter(v -> v.startsWith("张")).toArray();
        System.out.println(array);

        System.out.println("收集到Map-----");

        //收集到Map集合：键是老师名称，值是老师薪水
        Map<String, Double> map = teachers.stream().collect(Collectors.toMap(Teacher::getName, Teacher::getSalary));
        System.out.println(map);
    }
}
